#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys
from optparse import OptionParser

from readdensity import readdensity
from readfdensity import readfdensity
from grid_plane import PlaneGrid
from plane import Plane
from NikolGTK import NikolGTK
import fr_parser

optparser= OptionParser()
optparser.add_option('-m', dest='ismax',
                      help='use precomputed maxs points',
                      action='store_true',
                      default=False)                     
optparser.add_option('-s', dest='isscat',
                      help='use precomputed pairs of maxs and \
                      their dirrections',				
                      action='store_true',
                      default=False)                     
optparser.add_option('-b', dest='isbelt',
                      help='use precomputed belts',				
                      action='store_true',
                      default=False)                     
options, args = optparser.parse_args(sys.argv[1:])
name = args[0]

# chek if files exist
# TODO add if-conditions for density fdensity max and scatdir files if neccesary

# Parse the density grid files
sys.stdout.write("Read density file: %s.density\n" % name)
density, size, max_density_val = readdensity('%s.density' % name)

# prepare grid
grid = PlaneGrid(incr=[int(size), int(size)])
for pl in density:        
    grid.add(plane=Plane(pl[0], pl[1]), val=pl[2])
density_grid = grid.returnGrid()	

# Load precomputed pair of maxs
pairs = []
cpairs = []
if options.isscat:
    print "Read precomputed pairs"
    for i in range(20):
        fname = '%s.scatpair.%s' % (name, i) 
        if os.path.isfile(fname):
            f = open(fname, 'r')
            lines = f.readlines(); f.close
            cpairs = []
            for line in lines:
                if line[0] == '#': continue    
                val = fr_parser.parse_line(line)
                if val: cpairs.append(val)    
        if len(cpairs) > 0: 
            pairs.append([Plane(cpairs[0][0], abs(90-cpairs[0][1])), cpairs[0][2], \
                          Plane(cpairs[1][0], abs(90-cpairs[1][1])), cpairs[1][2]])    
        cpairs=[]
    #print "%s pairs was read" % len(pairs)
    #for pair in pairs: 
    #    print "%s %s %s | %s %s %s" % (pair[0].dir, abs(90-pair[0].dip), pair[1], pair[2].dir, abs(90-pair[2].dip), pair[3])
# Load belts
belts = []
print "Read belts file"
for i in range (20):
    fname = '%s.belt.%s' % (name, i)
    if os.path.isfile(fname):
        f = open(fname, 'r')
        lines = f.readlines(); f.close
        for line in lines:
            if line[0] == '#': continue    
            val = fr_parser.parse_line(line)
            if val: belts.append(Plane(val[0], val[1]))    
# Plot temporary background images for ortho and stereo
sys.stdout.write("Plot temporary background images.\n")
Width, Height, Radius = 600, 400, 200
PID = os.getpid()
os.system("echo '' | python fr_ortho.py -d %s.density -b -w1 -W%s -H%s -o /tmp/%sortho" % 
                                    (name, Width, Height, PID))
os.system("echo '' | python fr_stereo.py -f %s.fdensity -b -w2 -R%s -o /tmp/%sstereo" % 
                                    (name, Radius, PID))

# start GUI
a = NikolGTK(name, grid, PID, pairs, belts)    
a.main()

# Delete temporary files
os.system("rm /tmp/%s*" % PID)
